package com.example.anew.journey.mynewjourney.firstchangelogic.firstChangeHistoricalRecord.persenter;

import android.content.Context;

import com.example.anew.journey.mynewjourney.base.MyApplication;
import com.example.anew.journey.mynewjourney.bean.TakeDateBean;
import com.example.anew.journey.mynewjourney.config.RBConstants;
import com.example.anew.journey.mynewjourney.customview.MyHistoricalDataCustomView;
import com.example.anew.journey.mynewjourney.customview.MyHorizontalScrollView;
import com.example.anew.journey.mynewjourney.firstchangelogic.firstChangeCardiacDetection.bean.HistoricalDataViewBean;
import com.example.anew.journey.mynewjourney.firstchangelogic.firstChangeHistoricalRecord.model.FirstChangeHistoricalRecordModel;
import com.example.anew.journey.mynewjourney.firstchangelogic.firstChangeHistoricalRecord.model.ISFirstChangeHistoricalRecordModel;
import com.example.anew.journey.mynewjourney.firstchangelogic.firstChangeHistoricalRecord.view.ISFirstChangeHistoricalRecordView;
import com.example.anew.journey.mynewjourney.firstchangelogic.firstChangeHistoricalRecordDateList.bean.HistoricalRecordListBean;
import com.google.gson.Gson;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.Callback;

import java.util.ArrayList;
import java.util.List;

import cn.jesse.nativelogger.NLogger;
import okhttp3.Call;
import okhttp3.Response;

/**
 * 杭州惟君医疗科技有限公司09
 * Created by 20433 on 2018/3/28
 *
 * @desc 东风山河绿，春已不折柳，繁花看不尽，处处惹人留
 * @desc 佛祖镇楼，bug辟容易
 */

public class FirstChangeHistoricalRecordPersenter {
    private ISFirstChangeHistoricalRecordModel mISFirstChangeHistoricalRecordModel;
    private ISFirstChangeHistoricalRecordView mISFirstChangeHistoricalRecordView;

    private MyHistoricalDataCustomView tmyhistorcaldatecustomview;
    private MyHorizontalScrollView tmy_horizontalScrollview;
    private int p = 1;


    public FirstChangeHistoricalRecordPersenter(ISFirstChangeHistoricalRecordView mISFirstChangeHistoricalRecordView) {
        this.mISFirstChangeHistoricalRecordView = mISFirstChangeHistoricalRecordView;
        mISFirstChangeHistoricalRecordModel = new FirstChangeHistoricalRecordModel();
    }
    public void initDate(MyHorizontalScrollView my_horizontalScrollview, final MyHistoricalDataCustomView myhistorcaldatecustomview, final Context mcontxt){
        tmyhistorcaldatecustomview  = myhistorcaldatecustomview;
        tmy_horizontalScrollview = my_horizontalScrollview;
        my_horizontalScrollview.setOnScrollStopListner(new MyHorizontalScrollView.OnScrollStopListner() {
            @Override
            public void onScrollStoped() {

            }

            @Override
            public void onScrollToLeftEdge() {
            }

            @Override
            public void onScrollToRightEdge() {
                p++;
                loaddate( mcontxt, p);


            }
            @Override
            public void onScrollToMiddlePoint(HistoricalDataViewBean bean) {
                mISFirstChangeHistoricalRecordView.setMiddleDate(bean);
            }
        });
        loaddate(mcontxt, p);

    }
    public void loaddate(Context mcontxt,int p){
        if (getUserID(mcontxt) == null) return;
        String url = RBConstants.SERVER_URL + RBConstants.HISTORICAL_RECORD_LIST;
        OkHttpUtils
                .post()
                .url(url)
                .addParams("cus_id", getUserID(mcontxt).getUser_id())
                .addParams("p", p+"")
                .build()
                .execute(new Callback() {
                    @Override
                    public Object parseNetworkResponse(Response response, int id) throws Exception {
                        String NetworkResponseDate = response.body().string();
                        Gson gson = new Gson();
                       NLogger.e("TAG", "===8877=========NetworkResponseDate=================" + NetworkResponseDate);
                        final HistoricalRecordListBean historicalRecordListBean =   gson.fromJson(NetworkResponseDate, HistoricalRecordListBean.class);
                                if(historicalRecordListBean.errorCode == 200){
                                    List<HistoricalRecordListBean.DataBean> mlist =  historicalRecordListBean.data;
                                    if(mlist != null){
                                        final List<HistoricalDataViewBean>   mbean = new ArrayList<HistoricalDataViewBean>();
                                        if(mlist.size() != 0){
                                            for (HistoricalRecordListBean.DataBean dataBean : mlist) {
                                                try {
                                                    String add_time =  dataBean.add_time;
                                                    String[] arr =  add_time.split(" ");
                                                    String[] drr =  arr[0].split("-");
                                                    String result = dataBean.result;
                                                    int Yvalue =  Integer.parseInt(result);
                                                    HistoricalDataViewBean historicalDataViewBean =    new HistoricalDataViewBean(Yvalue,drr[1]+"/"+drr[2]);
                                                    mbean.add(historicalDataViewBean);
                                                } catch (NumberFormatException e) {}
                                            }
                                            //  mISFirstChangeHistoricalRecordView.setMiddleDate(mbean.get(mbean.size()/2));
                                            MyApplication.getmHandler().post(new Runnable() {
                                                @Override
                                                public void run() {
                                                    tmyhistorcaldatecustomview.setMlist(mbean);
                                                    tmy_horizontalScrollview.setMlist(mbean);
                                                }
                                            });
                                        }


                                    }

                                }


                        return null;
                    }

                    @Override
                    public void onError(Call call, Exception e, int id) {

                    }

                    @Override
                    public void onResponse(Object response, int id) {

                    }
                });

    }

    private TakeDateBean getUserID(Context mContext) {
        TakeDateBean bean = mISFirstChangeHistoricalRecordModel.TakeDate(mContext);
        if(bean == null) return null;
        return bean;
    }
}
